package py.una.pol.tesis.ii.emergenciasmovil.app.ui;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONObject;

import py.una.pol.tesis.ii.emergenciasmovil.app.R;
import py.una.pol.tesis.ii.emergenciasmovil.app.dto.Respuesta;
import py.una.pol.tesis.ii.emergenciasmovil.app.util.UtilParse;


public class LoginActivity extends ActionBarActivity {
    private static final String TAG = "LoginActivity";
    EditText usuario, contrasenha;
    private RequestQueue requestQueue;
    private JsonObjectRequest jsArrayRequest;
    // Progress dialog
    private ProgressDialog pDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Hide title bar
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_login);

        usuario = (EditText) findViewById(R.id.numDoc);
        contrasenha = (EditText) findViewById(R.id.contrasenha);

        Button button = (Button) findViewById(R.id.buttonAcceder);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                try {
                    if(usuario.getText().toString().trim().length() == 0 || contrasenha.getText().toString().trim().length() == 0){
                        Toast.makeText(LoginActivity.this, "Complete todos los datos.", Toast.LENGTH_SHORT).show();
                        return;
                    }

                    pDialog = new ProgressDialog(LoginActivity.this);
                    pDialog.setMessage("Por favor aguarde...");
                    pDialog.setCancelable(false);

                    showpDialog();
                    requestQueue = Volley.newRequestQueue(LoginActivity.this);

                    JSONObject jsonAdd = new JSONObject();
                    jsonAdd.put("usuario", usuario.getText().toString());
                    jsonAdd.put("password", contrasenha.getText().toString());

                    jsArrayRequest = new JsonObjectRequest(Request.Method.POST,
                            LoginActivity.this.getString(R.string.url_base) + LoginActivity.this.getString(R.string.post_login),
                            jsonAdd,
                            new Response.Listener<JSONObject>() {
                                @Override
                                public void onResponse(JSONObject response) {
                                    Respuesta resp = UtilParse.parseJson(response);
                                    hidepDialog();
                                    if(resp.getEstado().equals("0") && resp.getMensaje().equals("OK")){
                                        Intent appInfo = new Intent(LoginActivity.this, MenuDrawerActivity.class);
                                        startActivity(appInfo);
                                        return;
                                    }else{
                                        Toast.makeText(LoginActivity.this, resp.getMensaje(), Toast.LENGTH_LONG).show();
                                    }

                                }

                            },
                            new Response.ErrorListener() {
                                @Override
                                public void onErrorResponse(VolleyError error) {
                                    Toast.makeText(LoginActivity.this, R.string.error_servidor, Toast.LENGTH_LONG).show();
                                    Log.d(TAG, "Error Respuesta en JSON: " + error.getMessage());
                                    hidepDialog();
                                }
                            }

                    );

                    requestQueue.add(jsArrayRequest);

                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
    }

    private void showpDialog() {
        if (!pDialog.isShowing())
            pDialog.show();
    }

    private void hidepDialog() {
        if (pDialog.isShowing())
            pDialog.dismiss();
    }
}
